package com.lekang.service.impl;

import java.util.List;

import com.lekang.common.core.domain.entity.SysUser;
import com.lekang.common.utils.DateUtils;
import com.lekang.le.domain.NursingPlanLevelBinding;
import com.lekang.le.mapper.NursingPlanLevelBindingMapper;
import com.lekang.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lekang.mapper.NursingLevelYyhMapper;
import com.lekang.domain.NursingLevelYyh;
import com.lekang.service.INursingLevelYyhService;

/**
 * 护理等级Service业务层处理
 * 
 * @author yyh
 * @date 2025-10-13
 */
@Service
public class NursingLevelYyhServiceImpl implements INursingLevelYyhService 
{
    @Autowired
    private NursingLevelYyhMapper nursingLevelYyhMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private NursingPlanLevelBindingMapper nursingPlanLevelBindingMapper;

    /**
     * 查询护理等级
     * 
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevelYyh selectNursingLevelYyhById(Long id)
    {
        return nursingLevelYyhMapper.selectNursingLevelById(id);

    }

    /**
     * 查询护理等级列表
     * 
     * @param nursingLevelYyh 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevelYyh> selectNursingLevelYyhList(NursingLevelYyh nursingLevelYyh)
    {
        List<NursingLevelYyh> nursingLevelYyhs = nursingLevelYyhMapper.selectNursingLevelList(nursingLevelYyh);
        for(NursingLevelYyh nl : nursingLevelYyhs){
            Long creator = nl.getCreator();
            SysUser sysUser = sysUserMapper.selectUserById(creator);
            nl.setSysUser(sysUser);
//            System.out.println("查询结果：");
//            System.out.println(nl);
        }

        return nursingLevelYyhs;
    }

    //查询名字是否重复
    @Override
    public int selectNursingLevelName(NursingLevelYyh nursingLevelYyh)
    {
        List<NursingLevelYyh> nursingLevelYyhs = nursingLevelYyhMapper.selectNursingLevelList(nursingLevelYyh);
        for(NursingLevelYyh nl : nursingLevelYyhs){
            Long creator = nl.getCreator();
            SysUser sysUser = sysUserMapper.selectUserById(creator);
            nl.setSysUser(sysUser);
//            System.out.println("查询结果：");
//            System.out.println(nl);
        }
        if(nursingLevelYyhs.size() > 0){
            return 1;
        }
        else {
            return 0;
        }


    }

    /**
     * 新增护理等级
     * 
     * @param nursingLevelYyh 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevelYyh(NursingLevelYyh nursingLevelYyh)
    {
        nursingLevelYyh.setCreateTime(DateUtils.getNowDate());
        int i = nursingLevelYyhMapper.insertNursingLevel(nursingLevelYyh);
        List<NursingLevelYyh> nursingLevelYyhs = nursingLevelYyhMapper.selectNursingLevelList(nursingLevelYyh);
        NursingLevelYyh nursingLevelYyh1 = nursingLevelYyhs.get(0);
        long longValue = nursingLevelYyh1.getId();
        int intValue;

//        if (longValue >= Integer.MIN_VALUE && longValue <= Integer.MAX_VALUE) {
//            intValue = (int) longValue;
//        } else {
//            // 处理超出范围的情况，例如抛异常或使用默认值
//            throw new IllegalArgumentException("long值超出int范围");
//        }
        NursingPlanLevelBinding nursingPlanLevelBinding = new NursingPlanLevelBinding();
        nursingPlanLevelBinding.setLevelId(longValue);
        nursingPlanLevelBinding.setPlanId(nursingLevelYyh.getPlanId());
        nursingPlanLevelBindingMapper.insertNursingPlanLevelBinding(nursingPlanLevelBinding);



        return i;
    }

    /**
     * 修改护理等级
     * 
     * @param nursingLevelYyh 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevelYyh(NursingLevelYyh nursingLevelYyh)
    {
        return nursingLevelYyhMapper.updateNursingLevel(nursingLevelYyh);
    }

    /**
     * 批量删除护理等级
     * 
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelYyhByIds(Long[] ids)
    {
        return nursingLevelYyhMapper.deleteNursingLevelByIds(ids);
    }

    /**
     * 删除护理等级信息
     * 
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelYyhById(Long id)
    {
        return nursingLevelYyhMapper.deleteNursingLevelById(id);
    }
}
